package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.TColumn;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @ClassName: ColumnJdbcDao.java
 * @Description: 专业管理
 * @Date 2016-4-22 上午11:45:46
 */
@Repository
public class ColumnDaoImpl extends BaseDaoImpl<TColumn> {
    Logger logger = Logger.getLogger(ColumnDaoImpl.class);



    public int update(String name, int id, int unitId,int channelShort) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_column set column_name = ? ");
        arrList.add(name);
        sb.append(" WHERE   ");
        sb.append(" id = ? and unit_id = ?  ");
        arrList.add(id);
        arrList.add(unitId);
        return updateJdbc(sb.toString(), arrList);
    }

    public void delete(String id, int unidId) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_column set is_del =  1 ");
        sb.append(" WHERE   ");
        sb.append(" id = ? and unit_id = ?");
        arrList.add(id);
        arrList.add(unidId);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 查询栏目
     *
     * @param channelShort 频道号
     * @param unitId       单位id
     * @return
     */
    public List<Map<String, Object>> getColumnList(int channelShort, int unitId) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        sb.append("SELECT ");
        sb.append(" t.*,ifnull(t1.short_num,0) short_num");
        sb.append(" from t_column t ");
        sb.append(" LEFT JOIN t_channel_column t1 ON t1.t_column_id = t.id and t1.t_column_id =t.id ");
        sb.append(" LEFT JOIN t_channel t2 ON t1.t_channel_id = t2.id AND t2.channel_short = ? ");
        sb.append(" where t.unit_id = ? and t.is_del = 0");
        arrList.add(channelShort);
        arrList.add(unitId);
        return getListJdbc(sb.toString(), arrList);
    }

    public  List<Map<String,Object>> getColumnListByChannelId(int channelId,int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        sb.append("SELECT ");
        sb.append(" t.* ");
        sb.append(" from t_column t ");
        sb.append(" where  t.is_del = 0 AND t.channel_id = ? and unit_id =?");
        arrList.add(channelId);
        arrList.add(unitId);
        return getListJdbc(sb.toString(), arrList);
    }

    public  List<Map<String,Object>> getChannelColumnListByChannelId(int channelShort,int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        sb.append("SELECT ");
        sb.append(" t.*  ");
        sb.append(" from t_column t join t_channel_column t1 on t1.t_column_id = t.id");
        sb.append(" where  t.is_del = 0 AND t.unit_id = ? and  t.channel_id = ? order by t1.short_num");

        arrList.add(unitId);
        arrList.add(channelShort);
        return getListJdbc(sb.toString(), arrList);
    }

    public List<TColumn> getAllColumnList() {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("SELECT ");
        sb.append("* ");
        sb.append(" from t_Column where is_del = 0");


        return findList(sb.toString(), arrList);
    }

    public TColumn getColumnByName(String name,int unitId,int channelShort) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("SELECT ");
        sb.append("* ");
        sb.append(" from t_column where is_del = 0 and column_name = ? and unit_id = ?  ");
        arrList.add(name);
        arrList.add(unitId);
        List<TColumn> list = findList(sb.toString(), arrList);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据栏目id查询栏目
     * @param columns 栏目id，多个栏目逗号隔开
     * @return
     */
    public List listByChannelId(String columns){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("SELECT ");
        sb.append("* ");
        sb.append(" from t_column where is_del = 0");
        String[] columnArr = columns.split(",");
        sb.append(" and (");
        int i = 0;
        for(String id :columnArr ){
            if(!id.equals("")){
                sb.append(" id = ? ");
                arrList.add(id);
                if(i<columnArr.length-1){
                    sb.append(" or ");
                }
            }
            i++;
        }
        sb.append(" )");
        return findList(sb.toString(), arrList);
    }
}
